/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.service.battle.coreBattleLogic.soulCardLogic;

import com.dc.gameserver.proto.gameserverprotoVo.soulHurtResponse;
import com.dc.gameserver.serverCore.model.gamemodel.EquipmentModel;
import com.dc.gameserver.serverCore.model.gamemodel.ItemCell;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.battle.coreBattleLogic.AbstractSoulLogic;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import org.springframework.stereotype.Service;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:14-1-3 </br>
 *         Time:下午2:42 </br>
 *         Package:{@link com.dc.gameserver.serverCore.service.battle.coreBattleLogic.soulCardLogic}</br>
 *         Comment： 1.以伤换伤
 */
@Service
public class soulHurtLogic extends AbstractSoulLogic {

    /**
     * 实例化处理单个玩家出牌逻辑引用
     */
    @Override
    public void PostConstruct() {
        SOULS[0x1]=this;
    }

    /**
     * 处理不同神识牌对象
     *
     * @param p1
     * @param p2
     */
    @Override
    public boolean SoulHandle(GameCharacter p1, GameCharacter p2) {
        boolean status=false;
        double euqAdditional=0; //神识法宝
        if (p1 instanceof PlayerInstance){
            PlayerInstance player= (PlayerInstance) p1;
            ItemCell cell=player.getCellByLocation(0x12);//location is 18
            int equTypeID= cell.getItemTypeID();
            if (equTypeID!=0&&cell.getType()== 0x8){
                //神识法宝对象  装备对象
                EquipmentModel equipmentModel= ScriptFactory.equipmentModelFastMap.get(equTypeID);
//             <effectType ID="702" intro="穿戴后使用神牌增加以伤换伤概率" />
                if(equipmentModel!=null){
                    for (;;){
                        int effectTypeID=equipmentModel.getEffectType1();
                        if (effectTypeID== 0x2be){
                            euqAdditional=equipmentModel.getEffectPercent1();
                            break;
                        }
                        effectTypeID=equipmentModel.getEffectType2();
                        if (effectTypeID== 0x2be){
                            euqAdditional=equipmentModel.getEffectPercent2();
                            break;
                        }
                        effectTypeID=equipmentModel.getEffectType3();
                        if (effectTypeID== 0x2be){
                            euqAdditional=equipmentModel.getEffectPercent3();
                            break;
                        }else {
                            // 配置文件有问题，导致防止死循环
                            break;
                        }
                    }
                }
            }
        }
        if (GameFormula.soulHurtSuccess(p1.getSoul(), p2.getSoul(), p1.getOutCardsList().size(), euqAdditional)){
            status=true;
        }
        //使用成功
        if (status){
            //神识使用成功   神识攻击对方
            int hurt=GameFormula.InotherInjuryToInjuryHurt(p2.getCurrentShield()); //气血伤害值

            p2.setCurrentShield(p2.getCurrentShield()-hurt);
            CardBox cardBox= CardBoxManager.getCardBoxByID(p1.getCardBoxID());
            soulHurtResponse.Builder builder=soulHurtResponse.newBuilder();
            builder.setRoleID(p2.getID());
            builder.setHurt(hurt);
            cardBox.submitMessageToEveryOne(wrappedBufferShort(soulHurtResponse.msgID.ID_VALUE, builder.build()));

            //如果对方被干掉，那么回合结束
            if (p2.getCurrentShield()<=0){
                p2.setCurrentShield(0);
                //被攻击掉血
                cardBox.flushBloodAndMagic();
                cardBox.notifyPkResult(p1);
                CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
            }
        }
        return status;
    }
}
